package com.grao.waterschool.human.config;

import com.grao.waterschool.human.Properties.Knife4jProperties;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;

import java.util.Collections;
import java.util.Optional;

/**
 * @author limk
 */
@Configuration
@EnableConfigurationProperties(Knife4jProperties.class)
public class Knife4jConfig {

    private final Environment environment;
    private final Knife4jProperties properties;

    public Knife4jConfig(Environment environment, Knife4jProperties properties) {
        this.environment = environment;
        this.properties = properties;
    }

    @Bean
    public OpenAPI openApi() {
        String appName = environment.getProperty("spring.application.name");
        return new OpenAPI()
                .info(info(appName))
                .externalDocs(doc())
                .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
                .components(new Components()
                        .addSecuritySchemes("X-Authorization", new SecurityScheme()
                                .type(SecurityScheme.Type.APIKEY)
                                .name("X-Authorization")
                                .in(SecurityScheme.In.HEADER)));
    }

    @Bean
    public GroupedOpenApi userApi() {
        String[] paths = {"/**"};
        String[] packagedToMatch = {
                "com.grao.waterschool.human.controller"
        };
        return GroupedOpenApi.builder()
                .group(Optional.ofNullable(properties.getGroup().getName()).orElse("default"))
                .displayName(properties.getGroup().getDisplayName())
                .pathsToMatch(paths)
                .packagesToScan(packagedToMatch).build();
    }

    private Info info(@Nullable String appName) {
        String defaultName = (appName == null ? "" : appName) + "模块";
        String title = Optional.ofNullable(properties.getTitle())
                .orElse(defaultName);
        String description = properties.getDescription();
        Contact contact = new Contact().name(properties.getContact().getUser())
                .url(properties.getContact().getUrl())
                .email(properties.getContact().getEmail())
                .extensions(Collections.emptyMap());
        License license = new License()
                .name(properties.getLicense().getName())
                .url(properties.getLicense().getUrl());
        return new Info()
                .title(title)
                .description(description)
                .contact(contact)
                .license(license)
                .version(properties.getVersion());
    }

    private ExternalDocumentation doc() {
        return new ExternalDocumentation().description("SpringDoc Wiki Documentation")
                .url("https://springdoc.org/");
    }
}
